var express = require('express');
var router = express.Router();
var sqlQuery = require('../../module/sql');
var pag = require('mongoose-sex-page')


/* GET home page. */
router.get('/', async(req, res, next)=>{
  //导航
  let Nav = await getNav()
  //获取热门电影
  let RFilm = await getRFilm()
  //获取最新电影
  let ZFilm = await getZFilm()
  //经典台词1
  let Taici1 = await getTaici()
  //经典台词2
  let Taici2 = await getTaici1()
  //喜剧台词
  let XTaici = await getXTaici()
  //轮播图
  let Slide = await getSlide()

  let options = {Nav,RFilm,ZFilm,Taici1,Taici2,XTaici,Slide}


  res.render('home/index',options)
});



//获取导航
async function getNav(){
  let sqlStr = 'select * from nav where state = 1'
  let result = await sqlQuery(sqlStr);
      //结果返回
  return Array.from(result);

}

//获取热门电影
async function getRFilm(){
  let sqlStr1 = 'select * from film where state = 1 and  hot = 1'
  let result1 = await sqlQuery(sqlStr1);
  //结果返回
  return Array.from(result1);
}


//获取最新电影
async function getZFilm(){
  let sqlStr2 = 'select *  from film  order by id desc limit 12;'
  let result2 = await sqlQuery(sqlStr2)
  //结果返回
  return Array.from(result2)
}


//获取经典台词1
async function getTaici(){
  let sqlStr3 = 'select id,filmName,brief from film where state = 1 and hot = 1 limit 8'
  let result3 = await sqlQuery(sqlStr3)
  //结果返回
  return Array.from(result3)
}

//获取经典台词2
async function getTaici1(){
  let sqlStr4 = 'SELECT id,filmName,brief from film ORDER BY id DESC limit 8'
  let result4 = await sqlQuery(sqlStr4)
  //结果返回
  return Array.from(result4)
}


//喜剧台词
async function getXTaici() {
  let sqlStr5 = 'SELECT id,filmName,brief from film where Typeid = 1  ORDER BY id DESC limit 8'
  let result5 = await sqlQuery(sqlStr5)
  //返回结果
  return Array.from(result5)
 }


//轮播图
async function getSlide() { 
  let sqlStr6 = 'select routeImg,SlideName from slide where state = 1'
  let result6 = await sqlQuery(sqlStr6)
  //返回结果
  return Array.from(result6)
 }


 router.get('/fenapi',async(req, res, next)=>{
   let sqlStr7 = 'select  id,filmName,poster from film  where state = 1 ORDER BY id DESC'
   let result7 = await sqlQuery(sqlStr7)
   let sqlStr8 = "select count(id) as number from film"
   let result8 = await sqlQuery(sqlStr8)
   let count = result8[0].number
   //返回结果
   res.json({
    state:"ok",
    count:count,
    page:Math.ceil(count/30),
    content:Array.from(result7)
  })
 })


module.exports = router;